package com.ty.web.controller;

import com.ty.model.AjaxResult;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;
import java.util.Map;

/**
 * 海康门禁事件Controller
 *
 * @Author Tommy
 * @Date 2024/7/2
 */
@RestController
@RequestMapping("/rest/hikdoor")
@Slf4j
public class HikDoorEventController {

    @RequestMapping("/evt")
    public AjaxResult recordComplete(HttpServletRequest request, @RequestBody(required = false) String body, String event_log) throws Exception {
        log.info("Body内容：\r\n{}", body);
        System.out.println();

        log.info("事件内容: \r\n{}", event_log);
        System.out.println();

        System.out.println("请求参数:");
        Map<String, String[]> params = request.getParameterMap();
        for (Map.Entry<String, String[]> entry : params.entrySet()) {
            if (!entry.getKey().equalsIgnoreCase("event_log")) {
                System.out.println(entry.getKey());
                for (String v : entry.getValue()) {
                    System.out.println(v);
                }
                System.out.println();
            }
        }
        System.out.println();

        System.out.println("请求Header:");
        Enumeration<String> headerEm = request.getHeaderNames();
        while (headerEm.hasMoreElements()) {
            String key = headerEm.nextElement();
            System.out.println(key + " = " + request.getHeader(key));
        }
        System.out.println("\r\n----------------------------\r\n");

        return AjaxResult.success();
    }
}
